home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / uip / other / newmail.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-11  |  2.2 KB  |  110 lines

  1. /*
  2.  * newmail - check for new mail
  3.  *
  4.  * The users mailbox is checked to see if they have new mail. The determination
  5.  * is done in two steps.
  6.  *
  7.  *    1.    If a binary file from msg is found, the mailbox size from the
  8.  *        header is compared with size of the actual mailbox.
  9.  *    2.    Otherwise, the mailbox is scanned to find a message without a
  10.  *        Status: line in the header.
  11.  *
  12.  * AUTHOR
  13.  *    Edward C. Bennett <edward@engr.uky.edu>
  14.  */
  15. #include "util.h"
  16. #include <sys/stat.h>
  17. #include "../msg/msg.h"
  18.  
  19. char    mboxbuf[BUFSIZ], binboxbuf[BUFSIZ];
  20.  
  21. main()
  22. {
  23.     FILE        *fp, *fopen();
  24.     struct    stat    statb;
  25.     char        *ctime(), *getlogin();
  26.     int        fd, new;
  27.  
  28.     sprintf(mboxbuf, "%s/%s", mldfldir, getlogin());
  29.     if (stat(mboxbuf, &statb) == -1 || statb.st_size == 0)
  30.         exit(0);    /* No mail at all */
  31.  
  32.     sprintf(binboxbuf, "%s/._%s", mldfldir, getlogin());
  33.     if ((fd = open(binboxbuf, 0)) != -1)
  34.         new = uses_msg(fd, &statb);
  35.     else if ((fp = fopen(mboxbuf, "r")) != NULL)
  36.         new = uses_mail(fp);
  37.  
  38.     if (new)
  39.         printf("You have new mail\n");
  40.     else
  41.         printf("You have mail\n");
  42.  
  43.     exit(0);
  44. }
  45.  
  46. /*
  47.  * uses_msg - compare old and new mailbox sizes.
  48.  *
  49.  * The header from the binary box is read and the size field is compared with
  50.  * the size of the actual mailbox. If the mailbox is bigger we have new mail.
  51.  */
  52. uses_msg(fd, statp)
  53. int fd;
  54. struct stat *statp;
  55. {
  56.     struct    status    status;    /* from msg.h */
  57.  
  58.     read(fd, &status, sizeof(status));
  59.  
  60.     if (statp->st_size > status.ms_eofpos)
  61.         return(1);
  62.  
  63.     return(0);
  64. }
  65.  
  66. #define    NEITHER        0
  67. #define    INHEADER    1
  68. #define    INBODY        2
  69.  
  70. /*
  71.  * uses_mail - scan the mailbox for Status: header lines.
  72.  *
  73.  * The mailbox is read and the message headers examined. As soon as we find
  74.  * a message without a Status: line we know we have new mail.
  75.  */
  76. uses_mail(fp)
  77. FILE *fp;
  78. {
  79.     char    buf[BUFSIZ];
  80.     int    seen_status, state;
  81.  
  82.     state = NEITHER;
  83.     while (fgets(buf, BUFSIZ, fp) != NULL) {
  84.         if (strcmp(buf, delim1) == 0) {
  85.             switch (state) {
  86.             case NEITHER:
  87.                 state = INHEADER;
  88.                 seen_status = 0;
  89.                 break;
  90.             case INBODY:
  91.                 state = NEITHER;
  92.                 break;
  93.             }
  94.             continue;
  95.         }
  96.         if (strlen(buf) == 1)
  97.             if (seen_status == 0)
  98.                 return(1);
  99.             else {
  100.                 state = INBODY;
  101.                 continue;
  102.             }
  103.  
  104.         if (state == INHEADER && strncmp(buf, "Status:", 7) == 0)
  105.             seen_status++;
  106.     }
  107.  
  108.     return(0);
  109. }
  110.